package com.baidu.baidumap;

import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.Toast;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.model.LatLngBounds;
import com.baidu.mapapi.overlayutil.PoiOverlay;
import com.baidu.mapapi.search.core.PoiInfo;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.poi.OnGetPoiSearchResultListener;
import com.baidu.mapapi.search.poi.PoiBoundSearchOption;
import com.baidu.mapapi.search.poi.PoiCitySearchOption;
import com.baidu.mapapi.search.poi.PoiDetailResult;
import com.baidu.mapapi.search.poi.PoiResult;
import com.baidu.mapapi.search.poi.PoiSearch;

/**
 * 范围内搜索
 */
public class PoiSearchInCityDemo extends BaseActivity {
    private PoiSearch poiSearch;
    private int currentPageIndex = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        poiSearch = PoiSearch.newInstance();
        poiSearch.setOnGetPoiSearchResultListener(new MyListener());
        search();
    }

    private void search() {
        PoiCitySearchOption citySearchOption = new PoiCitySearchOption();
        citySearchOption.city("南通");
        citySearchOption.keyword("加油站");
        citySearchOption.pageNum(currentPageIndex);
        poiSearch.searchInCity(citySearchOption);



    }

    class MyListener implements OnGetPoiSearchResultListener{



        @Override
        public void onGetPoiResult(PoiResult poiResult) {
            if (poiResult==null|| SearchResult.ERRORNO.RESULT_NOT_FOUND==poiResult.error){
                Toast.makeText(getApplicationContext(),"未搜索到结果",Toast.LENGTH_SHORT).show();
                return;
            }
            String text = "共" + poiResult.getTotalPageNum() + "页，共"
                    + poiResult.getTotalPoiNum() + "条，当前第"
                    + poiResult.getCurrentPageNum() + "页，当前页"
                    + poiResult.getCurrentPageCapacity() + "条";

            Toast.makeText(getApplicationContext(), text, Toast.LENGTH_LONG).show();
            baiduMap.clear();// 清空地图所有的 Overlay 覆盖物以及 InfoWindow
            PoiOverlay overlay = new MyPoiOverLay(baiduMap);//搜索poi的覆盖物
            baiduMap.setOnMarkerClickListener(overlay);
            overlay.setData(poiResult);
            overlay.addToMap();
            overlay.zoomToSpan();
        }

        @Override
        public void onGetPoiDetailResult(PoiDetailResult result) {

        }


    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode==KeyEvent.KEYCODE_1){
            currentPageIndex++;
            search();
        }
        return super.onKeyDown(keyCode, event);
    }

    class MyPoiOverLay extends PoiOverlay{

        public MyPoiOverLay(BaiduMap baiduMap) {
            super(baiduMap);
        }

        @Override
        public boolean onPoiClick(int i) {
            PoiResult poiResult = getPoiResult();
            PoiInfo poiInfo = poiResult.getAllPoi().get(i);
            String text = poiInfo.name+","+poiInfo.address;
           Toast.makeText(getApplicationContext(),text,Toast.LENGTH_SHORT).show();
            return super.onPoiClick(i);
        }
    }
}
